package day02;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 输入一个数学计算表达式，如:1+2
 * 然后输出计算后的结果:1+2=3
 * 这里计算表达式只计算一次即可，可以使用加减乘除任意一个,可以进行小数运算。
 * @author Bonnie
 *
 */
public class Test07 {
    public static void main(String[] args) {
        String regex = "\\d+\\.?\\d*[+\\-*/]\\d+\\.?\\d*";
        String regexCal = "[+\\-*/]";
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入一个数学计算表达式，如:1+2,加减乘除都可，可以有小数，但仅能两个数运算：");
        String writeIn = scanner.next();
        if (writeIn.matches(regex)) {
            //符合规范,计算默认转成小数，因为字符里整型可以转成浮点型
            String cal = writeIn;
            cal = cal.replaceAll("[\\d\\.]+", " ");
            cal = cal.trim();
            String[] s = writeIn.split(regexCal);
            Double number1 = Double.parseDouble(s[0]);
            Double number2 = Double.parseDouble(s[1]);
            switch (cal){
                case "+":
                    System.out.println(number1 + number2);
                    break;
                case "-":
                    System.out.println(number1 - number2);
                    break;
                case "*":
                    System.out.println(number1 * number2);
                    break;
                case "/":
                    System.out.println(number1 / number2);
                    break;
                default:
                    System.out.println("重新输入");
            }
        } else {
            System.out.println("输入有误，请重新输入");
        }
    }

}
